package com.w3c.pragmatic.controller;

import com.w3c.pragmatic.domain.Response;
import com.w3c.pragmatic.entity.OrderInfo;
import com.w3c.pragmatic.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 测试redis 前端控制器
 *
 * @author wangdong
 * @since 2025-03-07
 */
@Slf4j
@RestController
@RequestMapping("/redis")
@Api(tags = "测试redis表控制层RedisController")
public class RedisController {

  @Resource private RedisUtil redisUtil;

  /**
   * redis操纵简单数据
   *
   * @return String
   */
  @ApiOperation(value = "redis操纵简单数据", httpMethod = "POST")
  @RequestMapping(
      value = "/val",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> setRedisVal() throws InterruptedException {
    log.info("==================================String的存取=================================");
    // 设置两个简单的值
    redisUtil.setVal("key1", "v1");
    redisUtil.setVal("key2", "v2", 3, TimeUnit.SECONDS);
    // 第一次取值
    String val1 = redisUtil.getStringVal("key1");
    String val2 = redisUtil.getStringVal("key2");
    log.info("String第一次取值，val1 = {}, val2 = {}", val1, val2);
    // 休眠5秒后第二次取值
    TimeUnit.SECONDS.sleep(5);
    String val11 = redisUtil.getStringVal("key1");
    String val22 = redisUtil.getStringVal("key2");
    log.info("String第二次取值，val11 = {}, val22 = {}", val11, val22);

    log.info("==================================Object的存取=================================");
    OrderInfo orderInfo = new OrderInfo();
    orderInfo.setOrderId("order00001");
    orderInfo.setOrderPrice(new BigDecimal("99.99"));
    redisUtil.setVal("key3", orderInfo);
    OrderInfo orderInfo1 = redisUtil.getClazzVal("key4", OrderInfo.class);
    log.info("Object第一次取值，object = {}", orderInfo1);

    return Response.ok();
  }

  /**
   * redis操纵List
   *
   * @return String
   */
  @ApiOperation(value = "redis操纵List", httpMethod = "POST")
  @RequestMapping(
      value = "/list/val",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> setRedisListVal() {

    log.info("==================================List的存取1=================================");
    /*List<String> element1 = Arrays.asList("A", "B", "C");
    String element2 = "D";
    Integer element3 = new Integer("5");
    redisUtil.setListVal("myList_2", element1);
    redisUtil.setListVal("myList_2", element2, 3, TimeUnit.SECONDS);
    redisUtil.setListVal("myList_2", element3);
    // 正常
    List<Object> myList = redisUtil.getListVal("myList_2");
    log.info("list第一次取值，object = {}", myList);

    // 过期
    TimeUnit.SECONDS.sleep(5);
    List<Object> myList2 = redisUtil.getListVal("myList_2");
    log.info("list第二次取值，object = {}", myList2);

    // 空值
    log.info("list第三次取值，object = {}", redisUtil.getListVal("myList_3"));*/

    log.info("==================================List的存取2=================================");
    OrderInfo orderInfo1 = new OrderInfo();
    orderInfo1.setOrderId("order00001");
    orderInfo1.setOrderPrice(new BigDecimal("11.11"));
    OrderInfo orderInfo2 = new OrderInfo();
    orderInfo2.setOrderId("order00002");
    orderInfo2.setOrderPrice(new BigDecimal("22.22"));
    redisUtil.setListVal("orderList", orderInfo1);
    redisUtil.setListVal("orderList", orderInfo2);
    redisUtil.setListVal("orderList", "orderList"); // 这种也可以添加，获取list的时候可以剔除
    // 正常
    List<OrderInfo> orderInfoList1 = redisUtil.getListVal("orderList", OrderInfo.class);
    log.info("list第四次取值，object = {}", orderInfoList1);

    // 空值
    log.info("list第五次取值，object = {}", redisUtil.getListVal("orderList2", OrderInfo.class));

    return Response.ok();
  }

  /**
   * redis操纵Hash
   *
   * @return String
   */
  @ApiOperation(value = "redis操纵Hash", httpMethod = "POST")
  @RequestMapping(
      value = "/hash/val",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> setRedisHashVal() {

    log.info("==================================Hash的存取1=================================");
    redisUtil.setHashVal("hash1", "map1", "val1", 10, TimeUnit.SECONDS);
    redisUtil.setHashVal("hash1", "map2", "val2", 10, TimeUnit.SECONDS);
    redisUtil.setHashVal("hash1", "map2", "val3", 10, TimeUnit.SECONDS); // 这里会覆盖
    Set<Object> hashKeySet = redisUtil.getHashKeySet("hash1");
    log.info("hash这里打印key==========={}", hashKeySet);
    log.info("hash这里打印key-空值是空列表==========={}", redisUtil.getHashKeySet("hash2"));
    List<Object> hashValList = redisUtil.getHashValList("hash1");
    log.info("hash这里打印val==========={}", hashValList);
    log.info("hash这里打印val-空值是空列表==========={}", redisUtil.getHashValList("hash2"));
    Object hashVal = redisUtil.getHashVal("hash1", "map2");
    log.info("hash这里打印val==========={}", hashVal);
    log.info("hash这里打印val-空值是null==========={}", redisUtil.getHashVal("hash1", "map22"));

    log.info("==================================Hash的存取2=================================");
    /*OrderInfo orderInfo1 = new OrderInfo();
    orderInfo1.setOrderId("order00001");
    orderInfo1.setOrderPrice(new BigDecimal("11.11"));
    OrderInfo orderInfo2 = new OrderInfo();
    orderInfo2.setOrderId("order00002");
    orderInfo2.setOrderPrice(new BigDecimal("22.22"));
    redisUtil.setHashVal("hash5", "map1", orderInfo1);
    redisUtil.setHashVal("hash5", "map2", orderInfo2);*/
    OrderInfo orderInfo = redisUtil.getHashVal("hash5", "map2", OrderInfo.class);
    log.info("hash5这里打印val==========={}", orderInfo);
    log.info(
        "hash5这里打印val-空值是null==========={}",
        redisUtil.getHashVal("hash6", "map2", OrderInfo.class));

    log.info("==================================Hash的存取3=================================");
    Map<Object, Object> hashMap = redisUtil.getHashMap("hash1");
    log.info("hash6这里打印Map==========={}", hashMap);
    log.info("hash6这里打印Map-空值是null==========={}", redisUtil.getHashMap("hash6"));

    // 泛型获取
    Map<String, OrderInfo> hash1 = redisUtil.getHashMap("hash5", OrderInfo.class);
    // Map<String, OrderInfo> hash1 = redisUtil.getHashMap("hash5", String.class, OrderInfo.class);
    log.info("hash6这里打印Map==========={}", hash1);
    log.info(
        "hash6这里打印Map-空值是null==========={}",
        redisUtil.getHashMap("hash6", String.class, OrderInfo.class));

    return Response.ok();
  }

  /**
   * redis操纵Set
   *
   * @return String
   */
  @ApiOperation(value = "redis操纵Set", httpMethod = "POST")
  @RequestMapping(
      value = "/set/val",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> setRedisSetVal() {
    return Response.ok();
  }

  /**
   * redis操纵Set
   *
   * @return String
   */
  @ApiOperation(value = "redis操纵Other", httpMethod = "POST")
  @RequestMapping(
      value = "/other/val",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<Long> setRedisOther() {
    // Long incre = redisUtil.increment("incre");
    Long decre = redisUtil.decrement("incre");
    return Response.ok(decre);
  }
}
